// mypage/setting/setting.js
var app=getApp();
var my=require("../../api/my");
var config=require("../../api/config");
var util=require("../../utils/util")

Page({
  /*页面的初始数据*/
  data: {
    info: '',
    mobile: '',
    mob: '',
    pop_show: true,
    bind: false,
    scope: '',
    token: ''
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    wx.getStorage({
      key: 'scope',
      success:function(res){
        that.setData({scope:res.data})
      }
    })
    that.setData({token:wx.getStorageSync('token'),userinfo:wx.getStorageSync('userinfo')})
    //
    that.esti_bind_wx();
    that.get_personal_dom();
  },
  onShow:function(){
    var that = this,mobile=app.globalData.phone?app.globalData.phone:wx.getStorageSync('userinfo').phone;
    var mob=mobile.replace(mobile.substring(3,7), "****")
    that.setData({mobile: mobile,mob: mob})
    if(app.globalData.change_psd&&!that.data.info.password){
      app.globalData.change_psd=false
      that.get_personal_dom();
    }
  },
  // 手机号
  get_phone:function(e){
    // console.log(e)
  },
  nav_replace:function(){
    wx.navigateTo({
      url: '../replacePhone/replacePhone',
    })
  },
  // 微信授权-绑定微信
  getUserInfo:function(e){
    // console.log(e)
    var that=this;
    if(e.detail){
      that.userinfo_decrypt_wx(e.detail)
    }else{
      util.page_show_toast('绑定失败',1500)
    }
  },
  // 弹窗
  open_pop:function(){
    this.setData({pop_show: !this.data.pop_show})
  },
  close_pop:function(){
    this.setData({pop_show: !this.data.pop_show})
  },
  // 密码
  nav_setPassword:function(){
    var that=this,info=that.data.info;
    wx.navigateTo({
      url: '../setPassword/setPassword',
    })
  },
  // 范围
  nav_mech:function(){
    wx.navigateTo({
      url: '../mechScope/mechScope',
    })
  },
  // 个人资料
  get_personal_dom:function(){
    var that=this,userinfo=wx.getStorageSync('userinfo');
    my.get_personal_dom({"id":userinfo.id}).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        that.setData({info:info})
      }
    })
  },
  // 解密
  userinfo_decrypt_wx:function(info){
    var that=this;
    var params={
      "openId": app.globalData.openId,
      "encryptedData": info.encryptedData,
      "iv": info.iv
    }
    config.decrypt_wx(params).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        wx.setStorageSync('unionId', info);
        
        //
        that.user_bind_wx(info)
      }
    })
  },
  // 退出登录
  user_unlogin:function(){
    var that=this,token=that.data.token;
    config.out_login(token).then(res=>{
      if(res.data.code==200){
        if(res.data.data){
          wx.removeStorageSync('unionId');
          wx.removeStorageSync('login_info');
          wx.removeStorageSync('userinfo');
          wx.removeStorageSync('token');
          wx.removeStorageSync('user_info');
          util.page_show_toast('退出登录成功',1500)
          setTimeout(function(){
            wx.navigateBack({
              delta: 1
            });
          },1500)
        }else{
          util.page_show_toast('退出登录失败',1500)
        }
      }
    })
  },
  // 判断是否绑定微信
  esti_bind_wx:function(){
    var that=this;
    var params={
      "unionId": that.data.userinfo.wxId,
      "openId": app.globalData.openId?app.globalData.openId:wx.getStorageSync('openId'),
      "bindType": "1",
      "thirdAppId": app.globalData.appId
    };
    config.esti_bind_wx(params).then(res=>{
      if(res.data.code==200){
        var bind=res.data.data;
        that.setData({bind: bind})
      }
    })
  },
  // 解绑微信
  user_unbind_wx:function(){
    var that=this;
    var params={
      "unionId": that.data.userinfo.wxId,
      "openId": app.globalData.openId?app.globalData.openId:wx.getStorageSync('openId'),
      "bindType": "1",
      "thirdAppId": app.globalData.appId
    };
    config.user_unbind_wx(params).then(res=>{
      if(res.data.code==200){
        that.close_pop();
        that.setData({bind: false})
        util.page_show_toast('解绑成功',2000)
        if(wx.getStorageSync('unionId')){
          wx.removeStorageSync('unionId');
          wx.removeStorageSync('userinfo');
          wx.removeStorageSync('token');
          wx.removeStorageSync('user_info');
          wx.navigateBack({
            delta: 1,
          })
        }
      }
    })
  },
  // 绑定微信
  user_bind_wx:function(info){
    var that=this,mobile=that.data.mobile,city_info=wx.getStorageSync('city');
    var params={
      "nickName": info.nickName,
      "phone": mobile,
      "avatar": info.avatarUrl,
      "sex": info.gender,
      "unionId": info.unionId,
      "openId": info.openId,
      "bindType": "1",
      "thirdAppId": app.globalData.appId,
      "province": city_info?city_info.province:'',
      "city": city_info?city_info.city:'',
      "area": city_info?city_info.district:'',
      "town": city_info?city_info.town:'',
      "address": city_info?city_info.address:''
    }
    config.user_bind_wx(params).then(res=>{
      if(res.data.code==200){
        that.setData({bind:true})
        util.page_show_toast('绑定成功',2000)
        // setTimeout(function(){
        //   wx.navigateBack({
        //     delta: 1
        //   });
        // },1500)
      }else{
        util.page_show_toast('绑定失败',2000)
      }
    })
  },
})